package demo.messaging.client.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;

import demo.messaging.client.core.ClientManager;
import demo.messaging.client.core.IMessageEventListener;
import demo.messaging.client.core.MessageEvent;
import demo.messaging.common.ChatMessage;
import demo.util_ui.client.ui.UIFactory;

public class ChatPanel extends DockPanel implements IMessageEventListener {

	private final Label title = new Label("Chat");

	private final FlowPanel messageHistory = new FlowPanel();

	private final DockPanel messageSendZone = new DockPanel();

	private final TextBox messageInput = new TextBox();

	private final Button messageSendButton = new Button("Send");

	private final ScrollPanel messageHistoryScroll;

	public ChatPanel() {
		super();
		this.addStyleName("chatPanel");

		this.messageHistory.addStyleName("messageHistory");
		this.messageInput.addStyleName("messageInput");

		this.messageSendZone.addStyleName("messageSendZone");
		this.messageSendZone.add(this.messageInput, DockPanel.CENTER);
		this.messageSendZone.add(this.messageSendButton, DockPanel.EAST);

		this.messageHistoryScroll = new ScrollPanel(this.messageHistory);
		this.messageHistoryScroll.addStyleName("messageHistoryScroll");

		this.add(this.title, DockPanel.NORTH);
		this.add(this.messageHistoryScroll, DockPanel.CENTER);
		this.add(this.messageSendZone, DockPanel.SOUTH);

		this.messageInput.addKeyUpHandler(new KeyUpHandler() {

			@Override
			public void onKeyUp(final KeyUpEvent event) {
				if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
					sendMessage();
				} else {
					updateSendButtonState();
				}
			}
		});

		this.messageSendButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(final ClickEvent event) {
				sendMessage();
			}
		});

		updateSendButtonState();
	}

	private void addMessageHistory(final String sender, final String content,
			final String cssSenderStyle, final String cssSenderMessageStyle) {

		HorizontalPanel p = new HorizontalPanel();
		p.addStyleName("chatMessageLine");
		p.add(UIFactory.createLabel(sender + ": ", cssSenderStyle));
		p.add(UIFactory.createLabel(content, cssSenderMessageStyle));
		this.messageHistory.add(p);
		this.messageHistoryScroll.ensureVisible(p);
	}

	@Override
	public void onMessageEvent(final MessageEvent event) {

		if (event.getMessage() instanceof ChatMessage) {
			addMessageHistory(event.getMessage().getSender(),
					((ChatMessage) event.getMessage()).getText(),
					"chatMessageRemoteSender", "chatMessageReceived");

		}
	}

	private void sendMessage() {
		final ChatMessage m = new ChatMessage();
		m.setText(this.messageInput.getText());
		m.setDestination(ClientManager.getClientManager()
				.getCurrentConnectedUser());

		ClientManager.getClientManager().sendMessageAsync(m);
		this.messageInput.setText("");
		addMessageHistory(ClientManager.getClientManager().getLogin(), m
				.getText(), "chatMessageLocalSender", "chatMessageSent");

	}

	private void updateSendButtonState() {
		if (this.messageInput.getText().trim().isEmpty()) {
			this.messageSendButton.setEnabled(false);
		} else {
			this.messageSendButton.setEnabled(true);
		}
	}

}
